﻿<Window x:Class="LetsTiltWPFFirstSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Let's tilt WPF" Height="560" Width="725">

    <Grid Name="RootLayout" Background="Gainsboro">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="220" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Click and drag for animation." Margin="10" FontWeight="Bold" FontSize="14" />
        
        <Grid Width="200" Height="200" Grid.Row="0" Grid.Column="0" Background="Black">
            <Viewport3D Grid.Column="0" x:Name="Viewport3DWrapper" Visibility="Hidden">
                <Viewport3D.Camera>
                    <PerspectiveCamera Position="0,0,0.5" LookDirection="0,0,-1" FieldOfView="90" />
                </Viewport3D.Camera>
                <Viewport3D.Children>
                    <ModelVisual3D>
                        <ModelVisual3D.Content>
                            <Model3DGroup>
                                <DirectionalLight Color="#444" Direction="0,0,-1" />
                                <AmbientLight Color="White" />
                            </Model3DGroup>
                        </ModelVisual3D.Content>
                    </ModelVisual3D>
                    
                    <ModelVisual3D>
                        <ModelVisual3D.Content>
                            <GeometryModel3D>
                                <GeometryModel3D.Geometry>
                                    <MeshGeometry3D TriangleIndices="0,1,2 2,3,0" TextureCoordinates="0,1 1,1 1,0 0,0"
                                                    Positions="-0.5,-0.5,0 0.5,-0.5,0 0.5,0.5,0 -0.5,0.5,0" />
                                </GeometryModel3D.Geometry>

                                <GeometryModel3D.Material>
                                    <DiffuseMaterial>
                                        <DiffuseMaterial.Brush>
                                            <VisualBrush Visual="{Binding ElementName=Host}" />
                                        </DiffuseMaterial.Brush>
                                    </DiffuseMaterial>
                                </GeometryModel3D.Material>

                                <GeometryModel3D.Transform>
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D x:Name="RotationTransform" Axis="{Binding RotationVector3D}" Angle="0" />
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </GeometryModel3D.Transform>
                            </GeometryModel3D>
                        </ModelVisual3D.Content>
                    </ModelVisual3D>
                </Viewport3D.Children>
            </Viewport3D>

            <Border x:Name="ContentWrapper">
                <Border x:Name="Host" Background="Transparent">
                    <Border.Triggers>
                        <EventTrigger RoutedEvent="Grid.MouseDown">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Viewport3DWrapper" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
                                    </ObjectAnimationUsingKeyFrames>

                                    <DoubleAnimation To="0" Duration="0:0:0.05" 
                                                     Storyboard.TargetName="ContentWrapper"
                                                     Storyboard.TargetProperty="Opacity" />

                                    <DoubleAnimation To="{Binding TiltAngle}" AccelerationRatio="0.3" DecelerationRatio="0.3"
                                                     BeginTime="0:0:0.05" Duration="0:0:1" Storyboard.TargetName="RotationTransform"
                                                     Storyboard.TargetProperty="Angle" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Grid.MouseUp">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Viewport3DWrapper" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
                                        <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{x:Static Visibility.Hidden}" />
                                    </ObjectAnimationUsingKeyFrames>

                                    <DoubleAnimation Duration="0:0:0.05" To="1" 
                                                     BeginTime="0:0:0.9"
                                                     Storyboard.TargetName="ContentWrapper"
                                                     Storyboard.TargetProperty="Opacity" />

                                    <DoubleAnimation To="0" AccelerationRatio="0.3" DecelerationRatio="0.3"
                                                     Duration="0:0:1" Storyboard.TargetName="RotationTransform"
                                                     Storyboard.TargetProperty="Angle" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Border.Triggers>

                    <Grid Width="200" Height="200" MouseMove="UIElement_OnMouseMove">
                        <Rectangle Fill="Red" Name="TestButton" />
                        <Image Source="minion.jpg" Margin="5" />
                    </Grid>
                    
                </Border>
            </Border>
        </Grid>
        
        <StackPanel Grid.Row="0" Grid.Column="1">
            <GroupBox Header="Touch coordinates">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <GroupBox Header="Current" Grid.Column="0" Margin="2">
                        <StackPanel Margin="5">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="X: " />
                                <TextBlock Text="{Binding CursorCoordinates.X}" VerticalAlignment="Center" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Y: " />
                                <TextBlock Text="{Binding CursorCoordinates.Y}" VerticalAlignment="Center" />
                            </StackPanel>
                        </StackPanel>
                    </GroupBox>
                    
                    <GroupBox Header="Normalized" Grid.Column="1" Margin="2">
                        <StackPanel Margin="5">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="X: " />
                                <TextBlock Text="{Binding NormalizedPoint.X}" VerticalAlignment="Center" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Y: " />
                                <TextBlock Text="{Binding NormalizedPoint.Y}" VerticalAlignment="Center" />
                            </StackPanel>
                        </StackPanel>
                    </GroupBox>
                </Grid>
            </GroupBox>
            
            <GroupBox Header="Rotation">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <GroupBox Header="Vector" Grid.Column="0" Grid.Row="0" Margin="2">
                        <StackPanel VerticalAlignment="Center" Margin="5">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="X: " />
                                <TextBlock Text="{Binding RotationVector3D.X}" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Y: " />
                                <TextBlock Text="{Binding RotationVector3D.Y}" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Z: " />
                                <TextBlock Text="{Binding RotationVector3D.Z}" />
                            </StackPanel>
                        </StackPanel>
                    </GroupBox>

                    <GroupBox Header="Options" Grid.Column="1" Grid.Row="0" Margin="2">
                        <StackPanel Margin="5">
                            <CheckBox IsChecked="{Binding IsZEnabled, Mode=TwoWay}" Content="Rotate Z" />
                            <StackPanel Orientation="Horizontal">
                                <Label Content="Max depression: " />
                                <Label Content="{Binding MaxDepression}" />
                            </StackPanel>
                            <Slider Maximum="50" Value="{Binding MaxDepression, Mode=TwoWay}" />
                        </StackPanel>
                    </GroupBox>

                    <GroupBox Header="Angle" Grid.Row="1" Grid.Column="0" Margin="2">
                        <StackPanel Margin="5">
                            <StackPanel Orientation="Horizontal">
                                <Label Content="Current:" />
                                <Label Content="{Binding Angle}" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <Label Content="AngleMagnitude:" />
                                <Label Content="{Binding AngleMagnitude}" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <Label Content="XAngleContribution:" />
                                <Label Content="{Binding XAngleContribution}" />
                            </StackPanel>
                        </StackPanel>
                    </GroupBox>

                    <GroupBox Header="Options" Grid.Column="1" Grid.Row="1" Margin="2">
                        <StackPanel Margin="5">
                            <StackPanel Orientation="Horizontal">
                                <Label Content="Tile angle: " />
                                <Label Content="{Binding TiltAngle}" />
                            </StackPanel>
                            <Slider Minimum="1" Maximum="45" Value="{Binding TiltAngle, Mode=TwoWay}" />
                        </StackPanel>
                    </GroupBox>

                </Grid>
            </GroupBox>
            
            <GroupBox Header="Other calculation data" Margin="2">
                <StackPanel Margin="5">
                    <StackPanel Orientation="Horizontal">
                        <Label Content="XMagnitude:" />
                        <Label Content="{Binding XMagnitude}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="YMagnitude:" />
                        <Label Content="{Binding YMagnitude}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="XDirection:" />
                        <Label Content="{Binding XDirection}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="YDirection:" />
                        <Label Content="{Binding YDirection}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="Depression:" />
                        <Label Content="{Binding Depression}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="GlobalOffsetZ:" />
                        <Label Content="{Binding GlobalOffsetZ}" />
                    </StackPanel>
                </StackPanel>
            </GroupBox>
        </StackPanel>
    </Grid>
</Window>
